/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 estimate_outcome.h

	$Header: /game/estimate_outcome.h $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "estimate_outcome.h"

#include "adventure_frame.h"
#include "adventure_map.h"
#include "combat_result.h"
#include "creature_array.h"
#include "external_string.h"
#include "format_string.h"
#include "player.h"
#include "replace_keywords.h"
#include "simulated_combat.h"

//------------------------------------------------------------------------------------------------
// determine correct outcome text
//------------------------------------------------------------------------------------------------
enum t_estimated_outcome
{
	k_outcome_severe_loss,
	k_outcome_loss,
	k_outcome_even,
	k_outcome_win,
	k_outcome_easy_win,
	
	k_outcome_count
};

static t_external_string const k_text_outcome[k_outcome_count] =
{
	"severe_loss.outcome",
	"loss.outcome",
	"even.outcome",
	"win.outcom",
	"easy_win.outcome"
};

static bool run_estimate( t_creature_array& attacker, t_creature_array& defender, float modifier )
{
	t_simulated_combat simulation( attacker, attacker.get_owner(), defender,
								   defender.get_owner(), *defender.get_map(),
								   defender.get_position(), defender.get_town() );
	
	simulation.set_effectiveness( false, modifier );
	simulation.set_effectiveness( true, 1.0 );
	simulation.run();
	
	return attacker_won( simulation.get_result() );
}

static t_estimated_outcome estimate_outcome( t_creature_array& attacker, t_creature_array& defender )
{
	if (!run_estimate( attacker, defender, 2.0 ))
		return k_outcome_severe_loss;
	if (!run_estimate( attacker, defender, 1.2f ))
		return k_outcome_loss;
	if (!run_estimate( attacker, defender, 1.0f / 1.2f ))
		return k_outcome_even;
	if (!run_estimate( attacker, defender, 1.0 / 2.0 ))
		return k_outcome_win;
	return k_outcome_easy_win;
}

//------------------------------------------------------------------------------------------------
// determine correct outcome text
//------------------------------------------------------------------------------------------------


std::string get_outcome_text( t_creature_array& enemy )
{
	t_adventure_map const&		map = *enemy.get_map();
	t_adventure_frame*			frame = enemy.get_adventure_frame();
	t_creature_array*			our_army = 0;
	t_player const&				player = map.get_player();

	our_army = frame->get_selected_creatures();
	if (our_army == 0)
		return "";
	if (same_team( our_army->get_owner(), enemy.get_owner()))
		return "";

	t_estimated_outcome outcome = estimate_outcome( *our_army, enemy );
	std::string			result = k_text_outcome[outcome];

	result = replace_keywords( result, "%hero_name", our_army->get_leader().get_name( true ));
	return result;
}
